package com.bysj.lms.controller;


import com.bysj.lms.common.ResultModel;
import com.bysj.lms.pojo.dto.CourseDto;
import com.bysj.lms.pojo.entity.Class;
import com.bysj.lms.pojo.entity.Course;
import com.bysj.lms.pojo.entity.DictionaryItem;
import com.bysj.lms.service.IClassService;
import com.bysj.lms.service.ICourseService;
import com.bysj.lms.service.IDictionaryItemService;
import com.bysj.lms.utils.EasyExcelUtil;
import com.bysj.lms.utils.NoModelDataListener;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaojiakai
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/course")
@Api(tags = "课程管理")
public class CourseController {

    @Autowired
    ICourseService courseService;
    @Autowired
    IClassService classService;
    @Autowired
    IDictionaryItemService dictionaryItemService;
    @ApiOperation("得到课程模型")
    @GetMapping()
    public Course getModel(){
        return new Course();
    }
    
    @ApiOperation("导入课程表")
    @PostMapping("/import")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ClassId",value = "班级id"),
            @ApiImplicitParam(name = "term",value = "学期")
    })
    public void importCourse(MultipartFile excel, int classId, String term) throws IOException {
        EasyExcelUtil.read(excel,new NoModelDataListener(classId,term,courseService));
    }


    
    @ApiOperation("导出课表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId",value = "班级id"),
            @ApiImplicitParam(name = "term",value = "学期")
    })
    @GetMapping("/export")
    public ResultModel export(HttpServletResponse response,Integer classId,String term) throws IOException {
        Class classInfo = classService.getById(classId);
        String filename = null;
        DictionaryItem major = dictionaryItemService.find("专业", classInfo.getMajor());
        filename = major.getName()+classInfo.getGrade()+classInfo.getName()+"班课程表";
        courseService.export(response,filename,classId,term);
        return ResultModel.success("导出成功",filename+".xlsx");
    }
    
    
    
    
    @ApiOperation(value = "课程列表",notes = "按班级,学期查询")
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classId",value = "班级id",required = true),
            @ApiImplicitParam(name = "term",value = "学期",required = true)
    })
    public ResultModel list(Integer classId,String term){
        List<Course> list = courseService.list(classId, term);
        return ResultModel.success("查询成功",list);
    }
    
//    @ApiOperation("根据期望周次，查询空白时间")
//    @GetMapping("/findBlackTime")
//    @ApiImplicitParams({
//            @ApiImplicitParam(value = "startWeek",name = "开始周"),
//            @ApiImplicitParam(value = "endWeek",name = "结束周")
//    })
//    public ResultModel findBlackTime(Integer startWeek,Integer endWeek){
//
//        return null;
//    }
    
    @ApiOperation("查看我的上机课")
    @GetMapping("/getMyCourse")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tid",value = "教师id，空为所有"),
            @ApiImplicitParam(name = "term",value = "学期，空为所有"),
            @ApiImplicitParam(name = "tname",value = "教师名"),
    })
    public ResultModel getMyCourse(String tid,String term,String tname){
        List<CourseDto> sc = courseService.getSC(tid, term,tname);
        return ResultModel.success("查询成功",sc);
    }

    @ApiOperation("修改")
    @PutMapping("edit")
    public ResultModel edit(@RequestBody Course course){
        boolean update = courseService.updateById(course);
        return update?ResultModel.success():ResultModel.failure();
    }
    
    @ApiOperation("新增")
    @PostMapping("add")
    public ResultModel add(@RequestBody Course course){
        boolean save = courseService.save(course);
        return save?ResultModel.success():ResultModel.failure();
    }
    
    @ApiOperation("删除")
    @PostMapping("delete")
    public ResultModel delete(Integer id){
        boolean delete = courseService.removeById(id);
        return delete?ResultModel.success():ResultModel.failure();
    }
}

